import request from "@/request/request.ts";
import { attribute, responseAttributeList, responseCategory } from "@/api/product/attributeApi/interface.ts";
import { categoryParams } from "@/views/product/attribute/interface/interface.ts";

enum Enum {
  fetchCategory1 = "/admin/product/getCategory1",
  fetchCategory2 = "/admin/product/getCategory2",
  fetchCategory3 = "/admin/product/getCategory3",
  fetchAttribute = "/admin/product/attrInfoList",
  deleteAttribute = "/admin/product/deleteAttr",
  addOrUpdateAttribute = "/admin/product/saveAttrInfo"
}

/* 获取一级分类的接口 */
export const fetchCategory1API: () => Promise<responseCategory> = (): Promise<responseCategory> => {
  return request({
    url: Enum.fetchCategory1,
    method: "GET",
  });
};

/* 获取二级分类的接口 */
export const fetchCategory2API: (category1Id: number) => Promise<responseCategory> = (category1Id: number): Promise<responseCategory> => {
  return request({
    url: `${Enum.fetchCategory2}/${category1Id}`,
    method: "GET",
  });
};

/* 获取三级分类的接口 */
export const fetchCategory3API: (category2Id: number) => Promise<responseCategory> = (category2Id: number): Promise<responseCategory> => {
  return request({
    url: `${Enum.fetchCategory3}/${category2Id}`,
    method: "GET",
  });
};

/* 获取属性的接口 */
export const fetchAttributeAPI: (categoryId: categoryParams) => Promise<responseAttributeList> = (categoryId: categoryParams): Promise<responseAttributeList> => {
  return request({
    url: `${Enum.fetchAttribute}/${categoryId.category1Id}/${categoryId.category2Id}/${categoryId.category3Id}`,
    method: "GET",
  });
};

/* 删除属性的接口 */
export const deleteAttributeAPI: (attributeId: number) => Promise<any> = (attributeId: number): Promise<any> => {
  return request({
    url: `${Enum.deleteAttribute}/${attributeId}`,
    method: "DELETE",
  });
};

/* 添加或修改属性的接口 */
export const addOrUpdateAttributeAPI: (data: attribute) => Promise<any> = (data: attribute): Promise<any> => {
  if (data.id) {
    /*{
      "id": 0
      "attrName": "string",
      "attrValueList": [{"attrId": 0, "id": 0,"valueName": "string"}
      ],
      "categoryId": 0,
      "categoryLevel": 3,
    }*/
    return request({
      url: `${Enum.addOrUpdateAttribute}`,
      method: "POST",
      data,
    });
  } else {
    /*{
      "attrName": "string",
      "attrValueList": [{"valueName": "string"}],
      "categoryId": 0,
      "categoryLevel": 3,
    }*/
    return request({
      url: `${Enum.addOrUpdateAttribute}`,
      method: "POST",
      data,
    });
  }
};
